﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.BS.Job.RequestClient;
using UFSoft.UBF.UI.ControlModel;
using UFSoft.UBF.UI.Engine.Builder;
using UFSoft.UBF.UI.WebControlAdapter;
using UFSoft.UBF.Util.Log;

namespace UFIDA.U9.Cust.XH.PlugUI
{
    /// <summary>
    /// 部门列表界面UI插件
    /// </summary>
    public class CustomerListUIWebPart : UFSoft.UBF.UI.Custom.ExtendedPartBase
    {

        #region 字段与属性                                       

        private UFIDA.U9.CBO.Pub.Customer.CustomerBListUIModel.CustomerBListUIFormWebPart part;

        #endregion
        public override void AfterInit(UFSoft.UBF.UI.IView.IPart Part, EventArgs args)
        {
            //首先调用原来的事件
            base.AfterInit(Part, args);
            part = Part as UFIDA.U9.CBO.Pub.Customer.CustomerBListUIModel.CustomerBListUIFormWebPart;

            if (part == null)
                return;

            #region 1.Toobar上添加按钮：页面扩展-加按钮

            //（2）、加入功能栏Card中
            IUFToolbar toolbar = (IUFToolbar)part.GetUFControlByName(part.TopLevelContainer, "Toolbar1");

            if (toolbar != null)
            {
                IUFButton btToOA = new UFWebButtonAdapter();
                string guid = Guid.NewGuid().ToString(); ;// 在数据库生成下GUID，或者修改下这里的值。SELECT NEWID()
                btToOA = UIControlBuilder.BuilderToolbarButton(toolbar, "True", "btToOA", "True", "True", 100, 28, "7", "", true, false,
                    guid, guid, guid);
                UIControlBuilder.SetButtonAccessKey(btToOA);
                btToOA.Text = "同步至OA（调度）";
                btToOA.ID = "btToOA";
                btToOA.AutoPostBack = true;
                btToOA.UIModel = part.Model.ElementID;
                ((UFWebToolbarAdapter)toolbar).Items.Add(btToOA as System.Web.UI.WebControls.WebControl);
                btToOA.Click += new EventHandler(btToOA_Click);


                IUFButton btToOA2 = new UFWebButtonAdapter();
                string guid2 = Guid.NewGuid().ToString();// 在数据库生成下GUID，或者修改下这里的值。SELECT NEWID()
                btToOA2 = UIControlBuilder.BuilderToolbarButton(toolbar, "True", "btToOA2", "True", "True", 100, 28, "9", "", true, false,
                    guid, guid, guid);
                UIControlBuilder.SetButtonAccessKey(btToOA2);
                btToOA2.Text = "同步至OA(即时)";
                btToOA2.ID = "btToOA2";
                btToOA2.AutoPostBack = true;
                btToOA2.UIModel = part.Model.ElementID;
                ((UFWebToolbarAdapter)toolbar).Items.Add(btToOA2 as System.Web.UI.WebControls.WebControl);
                btToOA2.Click += new EventHandler(btToOA2_Click);
            }
            #endregion





        }
        private static ILogger logger = LoggerManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private void btToOA2_Click(object sender, EventArgs e)
        {
            try
            {



                UFIDA.U9.Cust.XIHE.SyncCustomerToOABP.Proxy.CustomerToOAProxy proxy = new XIHE.SyncCustomerToOABP.Proxy.CustomerToOAProxy();
                string res = proxy.Do();
                logger.Error("UI 返回信息" + res);
                this.part.ShowWindowStatus(res, true);
            }
            catch (Exception ex)
            {

                this.part.ShowWindowStatus(ex.Message, true);

            }
            //string PageID = "390a732c-937a-484e-b63a-8a0f71ad2e69";
            //SubmitCallbackAction action = new SubmitCallbackAction();
            //action.Callback += new SubmitCallback(DepEvent);
            //this.part.CurrentState[SubmitCallbackAction.SUBMITCALLBACK] = action;
            //this.part.ShowModalDialog(PageID, "客户同步调度", "320", "195", null, null, false, false);                
        }
        private void btToOA_Click(object sender, EventArgs e)
        {
            string PageID = "390a732c-937a-484e-b63a-8a0f71ad2e69";
            SubmitCallbackAction action = new SubmitCallbackAction();
            action.Callback += new SubmitCallback(DepEvent);
            this.part.CurrentState[SubmitCallbackAction.SUBMITCALLBACK] = action;
            this.part.ShowModalDialog(PageID, "客户同步调度", "320", "195", null, null, false, false);
        }
        public void DepEvent()
        {
            UFIDA.U9.Cust.XIHE.SyncCustomerToOABP.Proxy.CustomerToOAProxy proxy = new XIHE.SyncCustomerToOABP.Proxy.CustomerToOAProxy();
            string strBPFullName = "UFIDA.U9.Cust.XIHE.SyncCustomerToOABP.CustomerToOA";
            long curPattem = long.Parse(this.part.CurrentState["RecurrencePatternID"].ToString());
            RequestSubmit rs = new RequestSubmit();
            rs.Application = 3000;
            rs.Priority = 1;
            rs.BPFullName = strBPFullName;
            rs.BPAgentObj = proxy;
            rs.SchedulerProject = curPattem;
            rs.RequestCode = "CustXIHECustomerTOOA";
            rs.RequestName = "CustXIHECustomerTOOA";
            rs.RequestDescription = "羲禾客户推送至OA";
            rs.Submit();
        }

    }
}
